<?php
/**
 * User: 自娱自乐自逍遥
 * Date: 14-10-14
 * Time: 下午1:21
 */

namespace Member\Controller;

use Common\Controller\MemberController;

class UserController extends MemberController{

    public function login(){
        $redirect_url = I('get.redirect_url',U('/'));
        if(IS_POST){
            $username = I('post.username');
            $password = I('post.password');
            if(empty($username))$this->error('用户名不能为空！');
            if(empty($password))$this->error('密码不能为空！');
            $rt = $this->user->login($username, $password);
            if($rt){
                $this->success('登陆成功！',$redirect_url);
            }else{
                $this->error($this->user->getError());
            }
        }else{
            $this->assign('redirect_url', $redirect_url);
            $this->display();
        }
    }

    public function register(){
        $redirect_url = I('get.redirect_url');
        if(empty($redirect_url))$redirect_url = '/';
        if(IS_POST){
            $username = I('post.username');
            $password = I('post.password');
            $repassword = I('post.repassword');

            if(empty($username))$this->error('用户名不能为空！');
            if(empty($password))$this->error('密码不能为空！');
            if(empty($repassword))$this->error('重复密码不能为空！');
            if($password !== $repassword)$this->error('两次密码输入不一致');

            $data = $this->user->create(array(
                'username'  => $username,
                'password'  => $password,
                'sex'       => 'U'
            ));
            if($data && ($uid=$this->user->add())){
                $rt = $this->user->login($username, $password);
                $this->success('注册成功！', $rt ? $redirect_url : U('/Member/User/Login'));
            }else{
                $this->error($this->user->getError());
            }
        }else{
            $this->assign('redirect_url', $redirect_url);
            $this->display();
        }
    }

    public function logout(){
        $this->user->logout();
        $this->success('你已退出登陆!',U('/'));
    }
} 